﻿using MyImdb.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace MyImdb.Common
{
    public static class FileHelper
    {
        public static string DatabaseBasePath = string.Empty;

        /// <summary>
        /// Saves to PNG.
        /// </summary>
        /// <param name="visual">The visual.</param>
        /// <param name="fileName">Name of the file.</param>
        public static void SaveToPng(UIElement visual, string fileName)
        {
            var encoder = new PngBitmapEncoder();
            SaveUsingEncoder(visual, fileName, encoder);
        }

        /// <summary>
        /// Saves the using encoder.
        /// </summary>
        /// <param name="visual">The visual.</param>
        /// <param name="fileName">Name of the file.</param>
        /// <param name="encoder">The encoder.</param>
        private static void SaveUsingEncoder(UIElement visual, string fileName, BitmapEncoder encoder)
        {
            RenderTargetBitmap bitmap = new RenderTargetBitmap(200, 300, 96, 96, PixelFormats.Pbgra32);
            bitmap.Render(visual);
            BitmapFrame frame = BitmapFrame.Create(bitmap);
            encoder.Frames.Add(frame);

            using (var stream = File.Create(fileName))
            {
                encoder.Save(stream);
            }
        }

        /// <summary>
        /// Saves the imdb title.
        /// </summary>
        /// <param name="content">The content.</param>
        /// <param name="fileName">Name of the file.</param>
        public static void SaveImdbTitle(string content, string fileName)
        {
            using (System.IO.StreamWriter writer = new System.IO.StreamWriter(fileName, true))
            {
                writer.Write(content);
            }
        }

        /// <summary>
        /// Creates the default database.
        /// </summary>
        public static void CreateSettings()
        {
            var settingsPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "myimdbsettings.txt");
            if (!File.Exists(settingsPath))
            {
                var appSettings = new Settings { SettingsPath = settingsPath, DatabasePath = string.Empty };
                var content = JsonConvert.SerializeObject(appSettings);

                using (System.IO.StreamWriter writer = new System.IO.StreamWriter(settingsPath, true))
                {
                    writer.Write(content);
                }
            }
        }

        /// <summary>
        /// Reads the settings.
        /// </summary>
        /// <returns></returns>
        public static Settings ReadSettings()
        {
            var settingsPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "myimdbsettings.txt");
            var settingsContent = File.ReadAllText(settingsPath);
            var settings = JsonConvert.DeserializeObject<Settings>(settingsContent);
            return settings;
        }

        /// <summary>
        /// Modifies the settings.
        /// </summary>
        /// <param name="content">The content.</param>
        public static void ModifySettings(string content)
        {
            var settingsPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "myimdbsettings.txt");
            if (File.Exists(settingsPath))
            {
                File.Delete(settingsPath);
                using (System.IO.StreamWriter writer = new System.IO.StreamWriter(settingsPath, true))
                {
                    writer.Write(content);
                }
            }
        }

        /// <summary>
        /// Reads the titles from specified path.
        /// </summary>
        /// <returns></returns>
        public static List<string> ReadTitles()
        {
            var titles = Directory.EnumerateFiles(DatabaseBasePath, "*.txt");
            return titles.ToList();
        }

        /// <summary>
        /// Reads the title.
        /// </summary>
        /// <param name="titlePath">The title path.</param>
        /// <returns></returns>
        public static ImdbItem ReadTitle(string titlePath)
        {
            var fullTitlePath = Path.Combine(DatabaseBasePath, titlePath);
            var titleContent = File.ReadAllText(fullTitlePath);
            var imdbTitle = JsonConvert.DeserializeObject<ImdbItem>(titleContent);
            return imdbTitle;
        }

        

        /// <summary>
        /// Determines whether [is database exist].
        /// </summary>
        /// <returns>
        ///   <c>true</c> if [is database exist]; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsDatabaseExist(string databasePath)
        {
            if (!string.IsNullOrEmpty(databasePath))
            {
                if (Directory.Exists(DatabaseBasePath))
                {
                    return true;
                }
                else
                {
                    return false;
                } 
            }
            else
            {
                return false;
            }
        }
    }
}
